<?php
namespace app\home\controller;

use app\BaseController;
use app\home\model\Demand;
use app\home\model\News;
use app\home\model\Seo;
use app\home\model\Supply;
use think\facade\View;

class Sd extends BaseController
{
    // 默认供应
    public function index()
    {
        return $this->supply();
    }
    public function supply()
    {
        //默认为推荐
        $this->paginationListAssign('supply', 'rec');
        return $this->baseAssign('supply');
    }
    public function supplyLast()
    {
        //最新
        $this->paginationListAssign('supply', 'date_desc');
        return $this->baseAssign('supply');
    }
    public function supplyLow()
    {
        //价格从低到高
        $this->paginationListAssign('supply', 'price');
        return $this->baseAssign('supply');
    }
    public function supplyHigh()
    {
        //价格从高到低
        $this->paginationListAssign('supply', 'price_desc');
        return $this->baseAssign('supply');
    }
    
    // 需求
    public function demand()
    {
        //默认为推荐
        $this->paginationListAssign('demand', 'rec');
        return $this->baseAssign('demand');
    }
    public function demandLast()
    {
        //最新
        $this->paginationListAssign('demand', 'date_desc');
        return $this->baseAssign('demand');
    }
    public function demandLow()
    {
        //价格从低到高
        $this->paginationListAssign('demand', 'price');
        return $this->baseAssign('demand');
    }
    public function demandHigh()
    {
        //价格从高到低
        $this->paginationListAssign('demand', 'price_desc');
        return $this->baseAssign('supply');
    }


    // 页面分页数据
    protected function paginationListAssign($type='supply', $order='date')
    {
        if($type == 'supply'){
            switch($order){
                case 'date':
                    $list = Supply::order('add_time')->paginate(10);
                    $page = $list->render();
                break;
                case 'date_desc':
                    $list = Supply::order('add_time desc')->paginate(10);
                    $page = $list->render();
                break;
                case 'price':
                    $list = Supply::order('price')->paginate(10);
                    $page = $list->render();
                break;
                case 'price_desc':
                    $list = Supply::order('price desc')->paginate(10);
                    $page = $list->render();
                break;
                case 'rec':
                    $list = Supply::where('rec', 1)->paginate(10);
                    $page = $list->render();
                break;
            }
        }else if($type == 'demand'){
            switch($order){
                case 'date':
                    $list = Demand::order('add_time')->paginate(10);
                    $page = $list->render();
                break;
                case 'date_desc':
                    $list = Demand::order('add_time desc')->paginate(10);
                    $page = $list->render();
                break;
                case 'price':
                    $list = Demand::order('price')->paginate(10);
                    $page = $list->render();
                break;
                case 'price_desc':
                    $list = Demand::order('price desc')->paginate(10);
                    $page = $list->render();
                break;
                case 'rec':
                    $list = Demand::where('rec', 1)->paginate(10);
                    $page = $list->render();
                break;
            }
        }
        
        View::assign('list', $list);
        View::assign('page', $page);
        
    }
    // 页面基础数据
    protected function baseAssign($type='supply')
    {
        View::assign('type', $type);
        View::assign('news', News::getByOrder('date_desc', 10));
        View::assign('kw', Seo::getKeywords());
        View::assign('desc', Seo::getDesc());
        return View::fetch('index');
    }
}
